#include "cNumber.h"
#include <vector>
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <cmath>

using namespace std;

cNumber::cNumber(int number)
{
    int tmpNumber[32];
    int cur = 0;

    if (number < 0)
    {
        this->sign = false;
        number = abs(number);
    }
    else
    {
        this->sign = true;
    }    

    while (number > 0)
    {
        tmpNumber[cur] = (number % 2) ? 1 : 0;
        number = number / 2;
        cur++;
    }

    this->iSize = cur;
    this->integer = new bool(cur);
    this->fSize = 0;
    this->fractional = new bool(0);

    for (int i = cur - 1, j = 0 ; i >= 0; i--, j++)
    {
        this->integer[j] = tmpNumber[i];
    }    
}

cNumber::cNumber(double number, int max)
{
    int tmpNumberI[32];
    int tmpNumberF[max];
    int curI = 0;
    int curF = 0;

    if (number < 0)
    {
        this->sign = false;
        number = abs(number);
    }
    else
    {
        this->sign = true;
    }

    int numberI = number;
    double numberF = number - numberI;

    while (numberI > 0)
    {
        tmpNumberI[curI] = (numberI % 2) ? 1 : 0;
        numberI = numberI / 2;
        curI++;
    }

    while (numberF != 0 && curF < max)
    {
        numberF *= 2;
        if (numberF >= 1)
        {
            tmpNumberF[curF] = 1;
            numberF -= 1;
        }
        else
        {
            tmpNumberF[curF] = 0;
        }
        curF++;
    }

    this->iSize = curI;
    this->integer = new bool(curI);
    this->fSize = curF;
    this->fractional = new bool(curF);

    for (int i = curI - 1, j = 0 ; i >= 0; i--, j++)
    {
        this->integer[j] = tmpNumberI[i];
    }

    for (int i = 0; i < curF; i++)
    {
        this->fractional[i] = tmpNumberF[i];
    }
}

void cNumber::show(int system)
{
    if (system == 2)
    {
        if (!this->sign)
            cout<<'-';

        for (int i = 0; i < this->iSize; i++)
        {
            cout<<(this->integer[i] ? '1' : '0');
        }

        if (this->fSize > 0)
        {
            cout<<',';
            for (int i = 0; i < this->fSize; i++)
            {
                cout<<(this->fractional[i] ? '1' : '0');
            }
        }
    }
    else if (system == 10)
    {
        if (!this->sign)
            cout<<'-';

        int sumI = 0;
        for (int i = this->iSize - 1, j = 0; i >= 0; i--, j++)
        {
            sumI += this->integer[i] * pow(2.0, j);
        }


        if (this->fSize > 0)
        {
            double sumF = 0;
            for (int i = 0; i < this->fSize; i++)
            {
                sumF += this->fractional[i] * pow(2.0, -1 * (i + 1));
            }
            cout<<(sumF + sumI);
        }
        else
        {
            cout<<sumI;
        }
    }
    cout<<endl;
}

cNumber operator+(cNumber number1, cNumber number2)
{
    int maxF = max(number1.fSize, number2.fSize);
    int tmpNumberF[maxF];
    bool next = false;

    for (int i = 0; i < maxF; i++)
    {
        bool x = number1.fractional[i];
        bool y = number2.fractional[i];

        if (x && y)
        {
            tmpNumberF[i] = next;
            next = true;
        }
        else if (x || y)
        {
            tmpNumberF[i] = !next;
        }
        else
        {
            tmpNumberF[i] = next;
            next = false;
        }
    }

    int maxI = max(number1->iSize, number2->iSize) + 1;
    int tmpNumberI[maxI];

    for (int i = 0; i < maxI - 1; i++)
    {
        bool x = number1.integer[i];
        bool y = number2.integer[i];

        if (x && y)
        {
            tmpNumberI[i] = next;
            next = true;
        }
        else if (x || y)
        {
            tmpNumberI[i] = !next;
        }
        else
        {
            tmpNumberI[i] = next;
            next = false;
        }
    }

    if (next)
    {
        tmpNumberI[maxI - 1] = true;
    }

    

    return NULL;

}

cNumber::~cNumber()
{
    delete this;
}